package com.bss.command;

import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.bss.beans.Patient;
import com.bss.dao.CheckInOutDao;
import com.bss.dao.PatientRecordDao;

public class PatientCommand extends AbstractCommand {
	
	private static byte [] image = null;
	
	private static boolean clicked = false;

	@Override
	public boolean execute(HttpServletRequest request, HttpSession session)
			throws Exception {
		// TODO Auto-generated method stub
		
		
		
		if(Integer.parseInt((session.getAttribute("action")).toString())==1)
		{
			 if(addpatient(request, session)==false)
			{
				
				return false;
			}
			else
			{
				session.removeAttribute("Error");
				return true;
			}
		}
		else if(Integer.parseInt((session.getAttribute("action")).toString())==2)
		{
			if(updatepatient(request, session)==false)
			{
				return false;
			}
			else
			{
				session.removeAttribute("Error");
				return true;
			}
			
		}
		else if(Integer.parseInt((session.getAttribute("action")).toString())==3)
		{
			if(uploadImage(request, session)==false)
			{
				return false;
			}
			else
			{
				return true;
			}
			
		}
		
		return false;
	}

	@Override
	public boolean falseforward(HttpServletResponse response,
			HttpSession session) throws Exception {
		// TODO Auto-generated method stub
		
		if(Integer.parseInt((session.getAttribute("action")).toString())==1)
		{
			session.setAttribute("welcomemsg", "UNABLE TO ADD NEW PATIENT");
			response.sendRedirect("patientrecord.jsp");
		}
		else if(Integer.parseInt((session.getAttribute("action")).toString())==2)
		{
			session.setAttribute("welcomemsg", "UNABLE TO EDIT NEW PATIENT");
			response.sendRedirect("patientrecord.jsp");
		}
		else if(Integer.parseInt((session.getAttribute("action")).toString())==3)
		{
			if(session.getAttribute("Error")==null)
			{
				session.setAttribute("welcomemsg", "UNABLE TO UPLOAD IMAGE");
				response.sendRedirect("patient.jsp");
			}
			else
			{
				response.sendRedirect("CHECKINOUT/addpatientphotograph.jsp");
			}
		}
		return false;
	}

	@Override
	public boolean trueforward(HttpServletResponse response, HttpSession session)
			throws Exception {
		// TODO Auto-generated method stub
		if(Integer.parseInt((session.getAttribute("action")).toString())==1)
		{
			session.setAttribute("welcomemsg", "NEW PATIENT SUCESSFULLY ADDED");
			response.sendRedirect("patient.jsp");
		}
		else if(Integer.parseInt((session.getAttribute("action")).toString())==2)
		{
			session.setAttribute("welcomemsg", " PATIENT RECORD SUCESSFULLY UPDATED");
			response.sendRedirect("patient.jsp");
		}
		else if(Integer.parseInt((session.getAttribute("action")).toString())==3)
		{
			session.setAttribute("welcomemsg", " PATIENT PHOTO SUCESSFULLY LOADED");
			response.sendRedirect("patient.jsp");
		}
		
		return false;
	}
	
	private boolean addpatient(HttpServletRequest request, HttpSession session) throws Exception
	{
		Patient patient=new Patient();
		int patientId=0;
		try
		{
			patient.setSPatientFirstName(request.getParameter("firstname"));
			patient.setSPatientLastName(request.getParameter("lastname"));
			patient.setSPatientMiddleName(request.getParameter("middlename"));
			patient.setSAddress(request.getParameter("address"));
			patient.setSContactNo(request.getParameter("contactno"));
			patient.setSIdCardType(request.getParameter("idcardtype"));
			patient.setSIdCardNo(request.getParameter("idcardno"));
			patient.setSSex(request.getParameter("patientsex"));
			patient.setSMedicalProblem(request.getParameter("medical"));
			patient.setSProfession(request.getParameter("profession"));
			patient.setSTataCaseNo(request.getParameter("tatacaseno"));
			
			
			
		}
		finally
		{
			
		}
		patientId=PatientRecordDao.AddNewPatient(patient);
		if(patientId==0)
		{
			return false;
		}
		else
		{
			//PatientRecordDao.updateLoadImage(patientId);
			return true;
		}
		
	}
	
	private boolean updatepatient(HttpServletRequest request, HttpSession session) throws Exception
	{
		Patient patient=new Patient();
		
		try
		{
			patient.setIPatientId(Integer.parseInt(request.getParameter("patientid")));
			patient.setSPatientFirstName(request.getParameter("firstname"));
			patient.setSPatientLastName(request.getParameter("lastname"));
			patient.setSPatientMiddleName(request.getParameter("middlename"));
			patient.setSAddress(request.getParameter("address"));
			patient.setSContactNo(request.getParameter("contactno"));
			patient.setSIdCardType(request.getParameter("idcardtype"));
			patient.setSIdCardNo(request.getParameter("idcardno"));
			patient.setSSex(request.getParameter("patientsex"));
			patient.setSMedicalProblem(request.getParameter("medical"));
			patient.setSProfession(request.getParameter("profession"));
			patient.setSTataCaseNo(request.getParameter("tatacaseno"));
			
			
			
			
			
			
		}
		finally
		{
			
		}
		
		return PatientRecordDao.updatePatient(patient);
		
	}
	
	
	public static ArrayList<String> loadFirstNames() throws Exception
	{
		return PatientRecordDao.loadFirstNames();
	}
	
	public static ArrayList<String> loadMiddleNames() throws Exception
	{
		return PatientRecordDao.loadMiddleNames();
	}
	
	public static ArrayList<String> loadLastNames() throws Exception
	{
		return PatientRecordDao.loadLastNames();
	}
	
	public static ArrayList<Integer> loadID() throws Exception
	{
		return PatientRecordDao.loadID();
	}
	public static Patient LoadPatient(int Id)throws Exception
	{
		return PatientRecordDao.LoadPatient(Id);
	}
	
	private boolean uploadImage(HttpServletRequest request, HttpSession session) throws Exception
	{
		return PatientRecordDao.updateLoadImage(Integer.parseInt(request.getParameter("patientId").toString()));
	}
	
	public static InputStream downloadImage(int patientId) throws Exception
	{
		return PatientRecordDao.downloadImage(patientId);
	}
	
	public static String getPatientName(int transactionId) throws Exception
	{
		return PatientRecordDao.getPatientName(CheckInOutDao.findPatientId(transactionId));
	}
	public static String getPatientNameFromPatId(int patientId) throws Exception
	{
		
		return PatientRecordDao.getPatientName(patientId);
	}

	public static void setImage(byte [] image) {
		PatientCommand.image = image;
	}

	public static byte [] getImage() {
		return image;
	}

	public static void setClicked(boolean clicked) {
		PatientCommand.clicked = clicked;
	}

	public static boolean isClicked() {
		return clicked;
	}
	
}
